﻿using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Runtime.CompilerServices;
using System.Text;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Windows.Forms;
using SolidWorks.Interop.sldworks;

namespace SwAddinHelpers.Sinks
{
    [ComImport, TypeLibType((short)0x1010), InterfaceType((short)2), Guid("83A33D32-37C5-11CE-BFD4-00400513BB57")]
    public interface DPartDocEvents
    {
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(1)]
        int RegenNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(2)]
        int DestroyNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(3)]
        int RegenPostNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(4)]
        int ViewNewNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(5)]
        int NewSelectionNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(6)]
        int FileSaveNotify([MarshalAs(UnmanagedType.BStr)] string FileName);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(7)]
        int FileSaveAsNotify([MarshalAs(UnmanagedType.BStr)] string FileName);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(8)]
        int LoadFromStorageNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(9)]
        int SaveToStorageNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(10)]
        int ActiveConfigChangeNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(11)]
        int ActiveConfigChangePostNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(14)]
        int ViewNewNotify2([MarshalAs(UnmanagedType.IDispatch)] object viewBeingAdded);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(15)]
        int LightingDialogCreateNotify([MarshalAs(UnmanagedType.IDispatch)] object dialog);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x10)]
        int AddItemNotify(int EntityType, [MarshalAs(UnmanagedType.BStr)] string itemName);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x11)]
        int RenameItemNotify(int EntityType, [MarshalAs(UnmanagedType.BStr)] string oldName, [MarshalAs(UnmanagedType.BStr)] string NewName);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x12)]
        int DeleteItemNotify(int EntityType, [MarshalAs(UnmanagedType.BStr)] string itemName);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x13)]
        int ModifyNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(20)]
        int FileReloadNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x15)]
        int AddCustomPropertyNotify([MarshalAs(UnmanagedType.BStr)] string propName, [MarshalAs(UnmanagedType.BStr)] string Configuration, [MarshalAs(UnmanagedType.BStr)] string Value, int valueType);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x16)]
        int ChangeCustomPropertyNotify([MarshalAs(UnmanagedType.BStr)] string propName, [MarshalAs(UnmanagedType.BStr)] string Configuration, [MarshalAs(UnmanagedType.BStr)] string oldValue, [MarshalAs(UnmanagedType.BStr)] string NewValue, int valueType);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x17)]
        int DeleteCustomPropertyNotify([MarshalAs(UnmanagedType.BStr)] string propName, [MarshalAs(UnmanagedType.BStr)] string Configuration, [MarshalAs(UnmanagedType.BStr)] string Value, int valueType);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x18)]
        int FeatureEditPreNotify([MarshalAs(UnmanagedType.IDispatch)] object EditFeature);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x19)]
        int FeatureSketchEditPreNotify([MarshalAs(UnmanagedType.IDispatch)] object EditFeature, [MarshalAs(UnmanagedType.IDispatch)] object featureSketch);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x1a)]
        int FileSaveAsNotify2([MarshalAs(UnmanagedType.BStr)] string FileName);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x1b)]
        int DeleteSelectionPreNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x1c)]
        int FileReloadPreNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x1d)]
        int BodyVisibleChangeNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(30)]
        int RegenPostNotify2([MarshalAs(UnmanagedType.IDispatch)] object stopFeature);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x1f)]
        int FileSavePostNotify(int saveType, [MarshalAs(UnmanagedType.BStr)] string FileName);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x20)]
        int LoadFromStorageStoreNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x21)]
        int SaveToStorageStoreNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x22)]
        int FeatureManagerTreeRebuildNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x23)]
        int FileDropPostNotify([MarshalAs(UnmanagedType.BStr)] string FileName);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x24)]
        int DynamicHighlightNotify(bool bHighlightState);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x25)]
        int DimensionChangeNotify([MarshalAs(UnmanagedType.IDispatch)] object displayDim);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x26)]
        int FileReloadCancelNotify(int ErrorCode);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x27)]
        int FileSavePostCancelNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(40)]
        int SketchSolveNotify([MarshalAs(UnmanagedType.BStr)] string featName);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x29)]
        int DeleteItemPreNotify(int EntityType, [MarshalAs(UnmanagedType.BStr)] string itemName);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x2a)]
        int ClearSelectionsNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x2b)]
        int EquationEditorPreNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x2c)]
        int EquationEditorPostNotify(bool Changed);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x2d)]
        int OpenDesignTableNotify([MarshalAs(UnmanagedType.IDispatch)] object DesignTable);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x2e)]
        int CloseDesignTableNotify([MarshalAs(UnmanagedType.IDispatch)] object DesignTable);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x2f)]
        int PromptBodiesToKeepNotify([MarshalAs(UnmanagedType.IDispatch)] object Feature, [MarshalAs(UnmanagedType.Struct)] ref object Bodies);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x30)]
        int AddDvePagePreNotify(int Command, [MarshalAs(UnmanagedType.IDispatch)] ref object PageToAdd);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x31)]
        int UnitsChangeNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(50)]
        int DestroyNotify2(int DestroyType);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x33)]
        int ConfigurationChangeNotify([MarshalAs(UnmanagedType.BStr)] string ConfigurationName, [MarshalAs(UnmanagedType.IDispatch)] object Object, int ObjectType, int changeType);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x34)]
        int SuppressionStateChangeNotify([MarshalAs(UnmanagedType.Interface)] Feature Feature, int NewSuppressionState, int PreviousSuppressionState, int ConfigurationOption, [MarshalAs(UnmanagedType.Struct)] ref object ConfigurationNames);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x35)]
        int ActiveViewChangeNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x36)]
        int FeatureManagerFilterStringChangeNotify([MarshalAs(UnmanagedType.BStr)] string FilterString);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x37)]
        int FlipLoopNotify([MarshalAs(UnmanagedType.IDispatch)] object TheLoop, [MarshalAs(UnmanagedType.IDispatch)] object TheEdge);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(12)]
        int AutoSaveNotify([MarshalAs(UnmanagedType.BStr)] string FileName);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(13)]
        int AutoSaveToStorageNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x38)]
        int FileDropPreNotify([MarshalAs(UnmanagedType.BStr)] string FileName);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x39)]
        int SensorAlertPreNotify([MarshalAs(UnmanagedType.IDispatch)] object SensorIn, int SensorAlertType);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x3a)]
        int UndoPostNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x3b)]
        int UserSelectionPreNotify(int SelType);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(60)]
        int ActiveDisplayStateChangePreNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x3d)]
        int ActiveDisplayStateChangePostNotify([MarshalAs(UnmanagedType.BStr)] string DisplayStateName);
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x3e)]
        int RedoPostNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x3f)]
        int RedoPreNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x40)]
        int UndoPreNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x41)]
        int WeldmentCutListUpdatePostNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x42)]
        int AutoSaveToStorageStoreNotify();
        [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x43)]
        int DragStateChangeNotify(bool State);
    }

    public class DPartDocEventsImpl : DPartDocEvents
    {
        private SwAddinHelpers.Contracts.ISwAddinBase _owner;

        public DPartDocEventsImpl(SwAddinHelpers.Contracts.ISwAddinBase owner)
        {
            _owner = owner;
        }

        public int RegenNotify()
        {
            return 0;
        }

        public int DestroyNotify()
        {

            return 0;
        }

        public int RegenPostNotify()
        {
            return 0;
        }

        public int ViewNewNotify()
        {
            return 0;
        }

        public int NewSelectionNotify()
        {
            Delegate d = _owner.MainAppEventSink.GetDelegate(Sinks.enumEventSink.Document_NewSelectionNotify);
            _owner.Debug.Manager.PostEvent(AddinManager.enumDebugMsgType.Callback, String.Join("", _owner.MainAppEventSink.GetInvocationList(d)));
            if (d != null) d.DynamicInvoke(new object[] { null, new SolidWorksEventArgs() });
            return 0;
        }

        public int FileSaveNotify(string FileName)
        {
            Delegate d = _owner.MainAppEventSink.GetDelegate(Sinks.enumEventSink.Document_FileSaveNotify);
            _owner.Debug.Manager.PostEvent(AddinManager.enumDebugMsgType.Callback, String.Join("", _owner.MainAppEventSink.GetInvocationList(d)));
            if (d != null) d.DynamicInvoke(new object[] { null, new SolidWorksEventArgs(Filename: FileName) });
            return 0;
        }

        public int FileSaveAsNotify(string FileName)
        {
            return 0;
        }

        public int LoadFromStorageNotify()
        {
            return 0;
        }

        public int SaveToStorageNotify()
        {
            return 0;
        }

        public int ActiveConfigChangeNotify()
        {
            return 0;
        }

        public int ActiveConfigChangePostNotify()
        {
            return 0;
        }

        public int ViewNewNotify2(object viewBeingAdded)
        {
            return 0;
        }

        public int LightingDialogCreateNotify(object dialog)
        {
            return 0;
        }

        public int AddItemNotify(int EntityType, string itemName)
        {
            return 0;
        }

        public int RenameItemNotify(int EntityType, string oldName, string NewName)
        {
            return 0;
        }

        public int DeleteItemNotify(int EntityType, string itemName)
        {
            return 0;
        }

        public int ModifyNotify()
        {
            return 0;
        }

        public int FileReloadNotify()
        {
            return 0;
        }

        public int AddCustomPropertyNotify(string propName, string Configuration, string Value, int valueType)
        {
            return 0;
        }

        public int ChangeCustomPropertyNotify(string propName, string Configuration, string oldValue, string NewValue, int valueType)
        {
            return 0;
        }

        public int DeleteCustomPropertyNotify(string propName, string Configuration, string Value, int valueType)
        {
            return 0;
        }

        public int FeatureEditPreNotify(object EditFeature)
        {
            return 0;
        }

        public int FeatureSketchEditPreNotify(object EditFeature, object featureSketch)
        {
            return 0;
        }

        public int FileSaveAsNotify2(string FileName)
        {
            return 0;
        }

        public int DeleteSelectionPreNotify()
        {
            return 0;
        }

        public int FileReloadPreNotify()
        {
            return 0;
        }

        public int BodyVisibleChangeNotify()
        {
            return 0;
        }

        public int RegenPostNotify2(object stopFeature)
        {
            return 0;
        }

        public int FileSavePostNotify(int saveType, string FileName)
        {
            return 0;
        }

        public int LoadFromStorageStoreNotify()
        {
            return 0;
        }

        public int SaveToStorageStoreNotify()
        {
            return 0;
        }

        public int FeatureManagerTreeRebuildNotify()
        {
            return 0;
        }

        public int FileDropPostNotify(string FileName)
        {
            return 0;
        }

        public int DynamicHighlightNotify(bool bHighlightState)
        {
            return 0;
        }

        public int DimensionChangeNotify(object displayDim)
        {
            return 0;
        }

        public int FileReloadCancelNotify(int ErrorCode)
        {
            return 0;
        }

        public int FileSavePostCancelNotify()
        {
            return 0;
        }

        public int SketchSolveNotify(string featName)
        {
            return 0;
        }

        public int DeleteItemPreNotify(int EntityType, string itemName)
        {
            return 0;
        }

        public int ClearSelectionsNotify()
        {
            return 0;
        }

        public int EquationEditorPreNotify()
        {
            return 0;
        }

        public int EquationEditorPostNotify(bool Changed)
        {
            return 0;
        }

        public int OpenDesignTableNotify(object DesignTable)
        {
            return 0;
        }

        public int CloseDesignTableNotify(object DesignTable)
        {
            return 0;
        }

        public int PromptBodiesToKeepNotify(object Feature, ref object Bodies)
        {
            return 0;
        }

        public int AddDvePagePreNotify(int Command, ref object PageToAdd)
        {
            return 0;
        }

        public int UnitsChangeNotify()
        {
            return 0;
        }

        public int DestroyNotify2(int DestroyType)
        {
            Delegate d = _owner.MainAppEventSink.GetDelegate(Sinks.enumEventSink.Document_DestroyNotify);
            _owner.Debug.Manager.PostEvent(AddinManager.enumDebugMsgType.Callback, String.Join("", _owner.MainAppEventSink.GetInvocationList(d)));
            if (d != null) d.DynamicInvoke(new object[] { null, new SolidWorksEventArgs(DestroyType: (SolidWorks.Interop.swconst.swDestroyNotifyType_e)DestroyType) });

            _owner.MainAppEventSink.DetachCurrentDocumentSink();

            return 0;
        }

        public int ConfigurationChangeNotify(string ConfigurationName, object Object, int ObjectType, int changeType)
        {
            return 0;
        }

        public int SuppressionStateChangeNotify(Feature Feature, int NewSuppressionState, int PreviousSuppressionState, int ConfigurationOption, ref object ConfigurationNames)
        {
            return 0;
        }

        public int ActiveViewChangeNotify()
        {
            return 0;
        }

        public int FeatureManagerFilterStringChangeNotify(string FilterString)
        {
            return 0;
        }

        public int FlipLoopNotify(object TheLoop, object TheEdge)
        {
            return 0;
        }

        public int AutoSaveNotify(string FileName)
        {
            return 0;
        }

        public int AutoSaveToStorageNotify()
        {
            return 0;
        }

        public int FileDropPreNotify(string FileName)
        {
            return 0;
        }

        public int SensorAlertPreNotify(object SensorIn, int SensorAlertType)
        {
            return 0;
        }

        public int UndoPostNotify()
        {
            return 0;
        }

        public int UserSelectionPreNotify(int SelType)
        {
            return 0;
        }

        public int ActiveDisplayStateChangePreNotify()
        {
            return 0;
        }

        public int ActiveDisplayStateChangePostNotify(string DisplayStateName)
        {
            return 0;
        }

        public int RedoPostNotify()
        {
            return 0;
        }

        public int RedoPreNotify()
        {
            return 0;
        }

        public int UndoPreNotify()
        {
            return 0;
        }

        public int WeldmentCutListUpdatePostNotify()
        {
            return 0;
        }

        public int AutoSaveToStorageStoreNotify()
        {
            return 0;
        }

        public int DragStateChangeNotify(bool State)
        {
            return 0;
        }
    }

}
